<?php
namespace app\common\lib;
use app\merchant\service\UserService;
use think\Controller;
use think\Config;
use think\Session;


class MyController extends Controller{

    public $_Constant;
    public $_Component;
    public $_Page;
    public $_Uid ;
    public $_Email;
    public $_User_type;
    public $_Account;
    public function _initialize()
    {
        Config::load(APP_PATH.'common/conf/config.php');
        $this->session_auto_clear(12000);
        $this->checkLogin();
        $this->_Constant = new Constant();
        $this->_Page = new Page();
        $this->_Component = new Component();
        $this->assign('USER_INFO', UserService::get_user());
    }

    public function returnData($code,$message,$data = []){
        $rs['message'] = $message;
        $rs['data'] = $data;
        $rs['code'] = $code;
        return json($rs);
    }


    /**
     * 权限检查，可设置测试控制器，无需登录控制器，需要完全权限的行为
     */
    private function checkLogin(){
        $controller = request()->controller();
        $module = request()->module();
        $action = request()->action();

        //#region 测试相关逻辑
        // 测试用控制器
        $testController = [
           // 'Borrower','Assets','Management','Merchant'
        ];


        //测试用的控制器，随意指定一个用户登录
        if(in_array($controller,$testController)){
            $uid = 1515747949; //测试用户id
            UserService::set_user($uid);
            $this->_initUserInfo();
            return;
        }
        //#endregion

        //#region 必须完全身份登录才能使用的Action
        $checkUserAction = [
            'Index/index'
        ];

        $this_action = $controller.'/'.$action;

        if(in_array($this_action,$checkUserAction)) {
            $this->_initUserInfo();
        }
        //#endregion

        //#region 判断是否需要登录
        // 无需登录的控制器
        $noLogin = [
            'Index'
        ];

        //不在里面则都需要登录
        if(!in_array($controller,$noLogin)){
            $this->_initUserInfo();
        }
        //#endregion
    }


    /**
     * 需要登录的情况下，初始化类的用户数据
     */
    private function _initUserInfo(){
        $userInfo = session('user_info');
        if($userInfo) {
            $this->_Uid = $userInfo['uid'];
            $this->_User_type = $userInfo['user_type'];
            $this->_Email = $userInfo['user_email'];
            $this->_Account = $userInfo['account'];
        }
        else{
            $this->redirect('index/login');
            return;
        }
    }

    /*
     * 设置延续时间
     */
    public  function session_auto_clear($expire = 3600){

        if(session('user_info')){

            $userInfo = session('user_info');

            if (empty($userInfo['expire'])){
                $userInfo['expire'] = time() + $expire;
            }

            if ($userInfo['expire'] < time()) {
                session('user_info', null);

            } else {

                $userInfo['expire'] = time() + $expire; // 刷新时间戳

                session('user_info',$userInfo);
            }
        }
    }


    //公用成功页面
    public function successView($str_redirectUrl = ''){
        $this->assign('redirect_url', $str_redirectUrl);
        return view('Widgets/success');
    }
    //公用提示信息页面
    public function messageView($msg ='', $str_redirectUrl = ''){
        $this->assign('redirect_url', $str_redirectUrl);
        $this->assign('msg', $msg);
        return view('Widgets/message');
    }
}